<?php
/*
Plugin Name: CMP Widget Support
Plugin URI: http://bizwebsky.com
Description: Widget hiện thị nhân viên hỗ trợ
Author: Phương CM
Version: 1.0
Author URI: http://google.com
*/

add_action('widgets_init', 'cmp_wg_support');
function cmp_wg_support(){
	register_widget('Cmp_wg_support');//class bên dưới
}

class Cmp_wg_support extends WP_Widget {

	function Cmp_wg_support() {
		$wget_options = array(
			'classname' => 'cmp_wg_support',
			'description' => 'Widget hiển thị nhân viên hỗ trợ'
		);
		$this->WP_Widget('cmp_wg_support_id', 'CMP Widget Support', $wget_options);
	}

	function form($instance)
	{
		$args = array('name' => '',
									'phone' => '',
			            'skype' => '',
									'yahoo' => '',
									'image_link' => '');
		$instance = wp_parse_args((array) $instance, $args);
		$title = esc_attr($instance['title']);

		$name = 'name';
		$phone = 'phone';
		$image_url = 'image_url';
		$skype = 'skype';
		$yahoo = 'yahoo';

		$instance[$name] = $instance[$name];
		$instance[$phone] = $instance[$phone];
		$instance[$image_url] = esc_url($instance[$image_url]);
		$instance[$skype] = $instance[$skype];
		$instance[$yahoo] = $instance[$yahoo];
		?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bizwebsky'); ?></label>
			<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
			       type="text" value="<?php echo $title; ?>"/>
		</p>

		<p>
			<label
				for="<?php echo $this->get_field_id($name); ?>"> <?php _e('Name ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($name); ?>"
			       name="<?php echo $this->get_field_name($name); ?>" value="<?php echo $instance[$name]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($phone); ?>"> <?php _e('Vị trí công việc - Số điện thoại ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($phone); ?>"
			       name="<?php echo $this->get_field_name($phone); ?>" value="<?php echo $instance[$phone]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($skype); ?>"> <?php _e('Skype ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($skype); ?>"
			       name="<?php echo $this->get_field_name($skype); ?>" value="<?php echo $instance[$skype]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($yahoo); ?>"> <?php _e('Yahoo ', 'bizwebsky'); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id($yahoo); ?>"
			       name="<?php echo $this->get_field_name($yahoo); ?>" value="<?php echo $instance[$yahoo]; ?>"/>
		</p>
		<p>
			<label
				for="<?php echo $this->get_field_id($image_url); ?>"> <?php _e('Avatar (kích thước tối thiểu 166x166)', 'bizwebsky'); ?></label>

			<?php
			if($instance[$image_url] != '') :
				echo '<img id="'.$this->get_field_id($instance[$image_url].'preview').'"src="'.$instance[$image_url].'"style="max-width:166px; max-height: 166px;" /><br />';
			endif;
			?>

			<input type="text" class="widefat custom_media_url" id="<?php echo $this->get_field_id($image_url); ?>"
			       name="<?php echo $this->get_field_name($image_url); ?>" value="<?php echo $instance[$image_url]; ?>"
			       style="margin-top:5px;"/>

			<input type="button" class="button button-primary custom_media_button" id="custom_media_button"
			       name="<?php echo $this->get_field_name($image_url); ?>" value="<?php _e('Upload Image', 'bizwebsky'); ?>"
			       style="margin-top:5px; margin-right: 30px;"
			       onclick="imageWidget.uploader( '<?php echo $this->get_field_id($image_url); ?>' ); return false;"/>
		</p>

	<?php
	}

	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);

		$name = 'name';
		$phone = 'phone';
		$image_url = 'image_url';
		$skype = 'skype';
		$yahoo = 'yahoo';

		$instance[$name] = $new_instance[$name];
		$instance[$phone] = $new_instance[$phone];
		$instance[$image_url] = esc_url_raw($new_instance[$image_url]);
		$instance[$skype] = $new_instance[$skype];
		$instance[$yahoo] = $new_instance[$yahoo];

		return $instance;
	}

	function widget($args, $instance)
	{
		extract($args);
		extract($instance);

		$title = isset($instance['title']) ? $instance['title'] : '';

		$name = 'name';
		$phone = 'phone';
		$image_url = 'image_url';
		$skype = 'skype';
		$yahoo = 'yahoo';

		$instance[$name] = isset($instance[$name]) ? $instance[$name] : '';
		$instance[$phone] = isset($instance[$phone]) ? $instance[$phone] : '';
		$instance[$skype] = isset($instance[$skype]) ? $instance[$skype] : '';
		$instance[$yahoo] = isset($instance[$yahoo]) ? $instance[$yahoo] : '';
		$instance[$image_url] = isset($instance[$image_url]) ? $instance[$image_url] : '';

//		echo $before_widget;
		ob_start();
		?>

		<li>
			<div class="feature">
				<img src="<?php echo $instance[$image_url]; ?>" style="max-width: 166px; max-height: 166px" />
				<div class="overlay">
					<p>
						<?php if(!empty($instance[$skype])): ?>
						<a href="skype:<?php echo $instance[$skype]; ?>?chat" class="skype"></a>
						<?php endif; ?>
						<?php if(!empty($instance[$yahoo])): ?>
						<a href="ymsgr:SendIM?<?php echo $instance[$yahoo]; ?>" class="yahoo"></a>
						<?php endif; ?>
					</p>
				</div>
			</div>
			<p class="name"><?php echo $instance[$name]; ?></p>
			<p class="details"><?php echo $instance[$phone]; ?></p>
		</li>

		<?php
		$content = ob_get_contents();
		ob_clean();
		echo $content;
	}

}
